package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.rank.RankReward;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class RankBaseData {
    private static Logger logger = LoggerFactory.getLogger(RankBaseData.class);
    private static ConcurrentHashMap<Integer, List<RankReward>> rankRewardByTypeMap = new ConcurrentHashMap<>(); // 通用的榜单奖励配置表<榜单类型, 榜单奖励配置列表>

    // 公共数据初始化
    public static void init(List<Future> futures) {
        futures.add(initRankReward());
    }

    /**
     * 初始化通用的榜单奖励配置表
     */
    public static Future<Void> initRankReward() {
        String table = "rank_reward";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, List<RankReward>> tmpTypeMap = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        RankReward entity = new RankReward(result.getJsonObject(i));
                        tmpTypeMap.computeIfAbsent(entity.getRankType(), k -> new ArrayList<>()).add(entity);
                    }
                    rankRewardByTypeMap = tmpTypeMap;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static ConcurrentHashMap<Integer, List<RankReward>> getRankRewardListMap() { return rankRewardByTypeMap; }

    // 获取榜单奖励配置
    public static RankReward getRankReward(int rankType, int rank) {
        List<RankReward> rankRewardList = rankRewardByTypeMap.get(rankType);
        if (rankRewardList == null) {
            return null;
        }
        for (RankReward rankReward : rankRewardList) {
            if (rank >= rankReward.getRankMin() && rank <= rankReward.getRankMax()) {
                return rankReward;
            }
        }
        return null;
    }

    /**
     * 根据榜单类型、榜单、榜单分组id获取榜单奖励配置
     * 注意：榜单分组id为0时，不进行分组过滤
     * @param rankType
     * @param rank
     * @param groupId
     * @return
     */
    public static RankReward getRankReward(int rankType, int rank, int groupId) {
        if (groupId == 0) { // 走原来的即可
            return getRankReward(rankType, rank);
        }

        List<RankReward> rankRewardList = rankRewardByTypeMap.get(rankType);
        if (rankRewardList == null) {
            return null;
        }

        List<RankReward> groupRankRewardList = rankRewardList.stream().filter(rankReward -> rankReward.getGroupId() == groupId).toList();
        for (RankReward rankReward : groupRankRewardList) {
            if (rank >= rankReward.getRankMin() && rank <= rankReward.getRankMax()) {
                return rankReward;
            }
        }
        return null;
    }

    /**
     * 获取榜单类型的最大排名
     * @param rankType
     * @return
     */
    public static int getRankTypeMaxNum(int rankType) {
        List<RankReward> rankRewardList = rankRewardByTypeMap.get(rankType);
        if (rankRewardList == null) {
            return 0;
        }
        return rankRewardList.stream().mapToInt(rankReward -> rankReward.getRankMax()).max().orElse(0);
    }
}
